(function(window){
  function Ajax(obj){
    this.url = obj.url;
    this.method = obj.method;
    if (obj.async) {
      this.async = obj.async;
    }else{
        this.async = true;
    }
    this.error = obj.error;
    this.success = obj.success;
    if (obj.data) {
      this.data = obj_to_str(obj.data);
    }else {
      this.data = null;
    }
    if (obj.header) {
      this.header = obj.header;
    }else{
      this.header = null;
    }
  }
  Ajax.prototype = {
    send:function(){
      var _this = this;

      var xmlhttp = new XMLHttpRequest() || new ActiveXObject("Microsoft.XMLHTTP");
      xmlhttp.open(this.method,this.url,this.async);
      if (this.header) {
        console.log(this.header);
        for(var key in this.header){
          var value = this.header[key];
          xmlhttp.setRequestHeader(key,value);
        }
      }

      xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
      xmlhttp.setRequestHeader("Accept","*/*");
      xmlhttp.onreadystatechange = function(){
          if (xmlhttp.readyState==4) {
              if (xmlhttp.status == 200) {
                  var res_type = xmlhttp.getResponseHeader("Content-Type");
                  //_this.success(JSON.parse(xmlhttp.response));
                  if(res_type.match("text/xml")!=null){
                    _this.success(xmlhttp.response);
                    return 0;
                  }else if(res_type.match("text/json")!=null){
                    _this.success(JSON.parse(xmlhttp.responseText));
                    return 0;
                  }else if(res_type.match("text/html")!=null){
                    _this.success(xmlhttp.responseText);
                    return 0;
                  }
                _this.success(JSON.parse(xmlhttp.responseText));
              }else{
                _this.error(JSON.parse(xmlhttp.responseText));
              }
          }
      };
      xmlhttp.send(this.data);
    }
  };
  function $(id){
      return document.querySelector(id);
  }
  function $$(idorclass){
      return document.querySelectorAll(idorclass);
  }
  function getCookie(){
      var cookie = {};
  	var cookies = document.cookie.split("; ");
  	for (var i = 0; i < cookies.length; i++) {
  		var onecookie = cookies[i].split("=");
  		cookie[onecookie[0]] = onecookie[1];
  	}
      return cookie;
  }
  function prompt(message){
    $('#state').innerHTML = message;
    show($('#prompt-outer'));
    setTimeout(function(){
      hide($('#prompt-outer'));
    },1300);
  }
  function obj_to_str(obj){
      var data = "";
      for(var key in obj){
          str= key + "=" + obj[key];
          data+=str+"&";
      }
      return data;
  }
  function eleAddEvent(element,method,func,mode){
    if(document.addEventListener){
      element.addEventListener(method,func,mode);
    }else{
      element.attachEvent(method,func,mode);
    }
  }
  function elesAddEvent(elements,method,func,mode){
    for(var i=0;i<elements.length;i++){
      eleAddEvent(elements[i],method,func,mode);
    }
  }
  function addClass(node,classStr){
    node.classList.add(classStr);
  }
  function removeClass(node,classStr){
    node.classList.remove(classStr);
  }
  function toggleClass(node,class1,class2){
    removeClass(node,class2);
    addClass(node,class1);
  }
  function show(node){
    node.style.display = 'block';
  }
  function hide(node){
    node.style.display = 'none';
  }

  function isEmptyObject(e) {
    var t;
    for (t in e)
      return !1;
    return !0
  }


  //模板渲染
  function render(template,obj){
      var result = template;
      var reg0 = /\{([^\{\}]+)\}/g;
      var matchArr = template.match(reg0);
      for (var i = 0; i < matchArr.length; i++) {
          var matchStr = matchArr[i].substr(1,matchArr[i].length-2);
          var indexStrArr = matchStr.split(".");
          var now = obj[indexStrArr[0]];
          for (var j = 1; j < indexStrArr.length; j++) {
              now = now[indexStrArr[j]];
          }
          result = result.replace(matchArr[i],now);
      }
      return result;
  }
  window.$ = $;
  window.$$ = $$;
  window.Ajax = Ajax;
  window.obj_to_str = obj_to_str;
  window.render = render;
  window.eleAddEvent = eleAddEvent;
  window.elesAddEvent = elesAddEvent;
  window.prompt = prompt;
  window.addClass = addClass;
  window.removeClass = removeClass;
  window.toggleClass = toggleClass;
  window.show = show;
  window.hide = hide;
})(window);

